<!--
 * @Description: 
 * @Author: rendc
 * @Date: 2024-11-15 08:36:27
 * @LastEditors: rendc
 * @LastEditTime: 2024-11-18 09:19:59
-->
<template>
  <div class="main">
    <el-row>
      <el-col class="myCol" :span="2">
        2
      </el-col>
      <el-col class="myCol" :span="2">
        2
      </el-col>
      <el-col class="myCol" :span="20">
        20
      </el-col>
    </el-row>
    <el-row>
      <el-col class="myCol" :span="4">
        <el-button type="success" @click="handleEdit()">新增</el-button>
      </el-col>
      <el-col class="myCol" :span="20">
        <el-input v-model="searchText" style="width: 120px" placeholder="请输入学号查询" />
        <!-- <el-button type="primary" @click="handleSearch()">搜索</el-button>
    <el-input v-model="searchText" style="width: 120px" placeholder="请输入姓名查询" /> -->
        <el-button type="primary" @click="handleSearch()">搜索</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData" border style="width: 50%">
      <el-table-column prop="id" label="学号" width="180" />
      <el-table-column prop="name" label="姓名" width="180" />
      <el-table-column prop="score" label="分数" width="180" />
      <el-table-column prop="desc" label="评语" />
      <el-table-column fixed="right" label="Operations" min-width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑模态框 开始 -->
    <el-dialog v-model="dialogFormVisible" :title="title" width="500">
      <!-- {{ form }} -->
      <el-form :model="form">
        <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="form.id" :disabled="title == '编辑' ? true : false" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input-number :min="0" :max="100" v-model="form.score" autocomplete="off" />
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input v-model="form.desc" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="editUser">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 编辑模态框 结束 -->

    <!-- 路由跳转 -->
    <div class="myRouter">
      <RouterLink to="/">Go to Home</RouterLink>
      <RouterLink to="/about">Go to About</RouterLink>
      <RouterLink to="/login">Go to Login</RouterLink>
    </div>
    <!-- 匹配到的路由展示的位置 -->
    <RouterView />
  </div>
</template>

<script setup>
import { ref } from 'vue'
const tableData = ref([
  {
    id: '001',
    name: '张三',
    score: 90,
    desc: "很优秀"
  },
  {
    id: '002',
    name: '李四',
    score: 80,
    desc: "优秀"
  },
  {
    id: '003',
    name: '王五',
    score: 70,
    desc: "良好"
  },
])
const formLabelWidth = '140px'
const form = ref({});
// 搜索框
const searchText = ref('');
// 编辑&新增的模态框
const dialogFormVisible = ref(false);
// 编辑&新增模态框的title
const title = ref("编辑");
function handleDelete (id) {
  // console.log('🤡 CC - handleDelete - id:', id);
  // 发送请求 delStudentById(id);
  tableData.value.forEach((e, index) => {
    if (e.id == id) {
      // console.log('🤡 CC - tableData.value.forEach - index:', index);
      // console.log('🤡 CC - handleDelete - e:', e);
      // 根据下标删除数组元素
      tableData.value.splice(index, 1);
    }
  })
}
// 搜索
const handleSearch = () => {
  console.log('🤡 CC - handleSearch - searchText.value:', searchText.value);
  // 发送请求到后台
  // findStudentById(searchText.value);
  // 将后台返回的数据渲染到页面上
  // tableData.value = res.data;
  // 模拟操作 - 遍历数组 找id == searchText.value的值

}
// const editText = "编辑";
// 打开编辑表单
function handleEdit (row) {
  // console.log('🤡 CC - handleEdit - row:', row);
  if (row) {
    // 编辑
    // console.log('🤡 CC - handleEdit - 编辑:');
    form.value = row;
    title.value = "编辑";
  } else {
    // 新增
    // console.log('🤡 CC - handleEdit - 新增:');
    title.value = "新增";
    form.value = {};
  }
  dialogFormVisible.value = true
}
// 提交编辑表单
function editUser () {
  if (title.value == '编辑') {
    // 编辑
    // 将数据发送到后台
    // editStudentById(form.value);
  } else {
    // 新增
    // 将数据发送到后台
    // addStudent(form.value);
    // 模拟操作
    // console.log('🤡 CC - editUser - form.value:', form.value);
    tableData.value.push(form.value);
  }

  dialogFormVisible.value = false
}
</script>

<style scoped>
.main {
  height: 100%;
  text-align: center;
  /* border: 1px solid red; */
  padding-left: 25%;
  padding-top: 25%;
}

.myCol {
  /* border: 1px solid red; */
  text-align: left;
}
</style>

<style lang="less"></style>
